package com.graduate.twentyone.mapper;

import com.graduate.twentyone.domain.Promission;
import com.graduate.twentyone.domain.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UsersMapper {

    //    通过用户名查询
    @Select("select * from users where uname = #{uname}")
    Users login(String uname);

    //    通过用户名查询权限
    @Select("SELECT pname FROM promission WHERE pid IN \n" +
            "(SELECT pid FROM r_p WHERE rid =\n" +
            "(SELECT rid FROM u_r WHERE uid = \n" +
            "(SELECT uid FROM users WHERE uname = #{uname})  ) )")
    List<Promission> loginpro(String uname);


    //    通过用户名查询身份
    @Select("SELECT rname FROM roles WHERE rid = " +
            "(SELECT rid FROM u_r WHERE uid =  " +
            "(SELECT uid FROM users WHERE uname = #{uname}))")
    String loginrname(String uname);

    //用户注册
    @Insert("insert into users values(null,#{uname},#{pwd},#{tname},1,0)")
    Integer addOne(Users users);

    //用户权限添加
    @Insert("insert into u_r values(null,#{uid},#{rid})")
    Boolean addPremiss(@Param("uid") Integer uid, @Param("rid") Integer rid);

    //用户权限查找
    @Select("select * from u_r where uid=#{uid}")
    Boolean queryPremiss(Integer uid);

    //用户权限更改
    @Update("update u_r set rid=#{rid} where uid=#{uid}")
    Boolean updatePremiss(@Param("rid") Integer rid, @Param("uid") Integer uid);

    //删除用户
    @Update("update users set status = 0 where uid=#{uid}")
    Boolean deleteOneUser(Integer uid);

    //    用户修改
    @Update("update users set uname=#{uname},pwd=#{pwd},tname=#{tname} where uid=#{uid}")
    Boolean updateUser(Users users);

    //    用户查询
    @Select("SELECT users.`uid`,uname,pwd,tname,rid,rname,users.`status` FROM users  LEFT JOIN (SELECT uid,u_r.rid,rname FROM u_r LEFT JOIN roles ON u_r.`rid`=roles.`rid`) ss ON users.`uid`=ss.uid where STATUS=1")
    List<Users> selectUsers();

    //    获取第一行数据
    @Select("select * from users order by  uid  desc limit 1")
    Users selectooo();


    //通过id 查询信息
    @Select("select * from users where uid=#{uid}")
    Users selectbyUid(Integer uid);

    //    设置在线状态
    @Update("update users set online=#{online} where uname=#{uname}")
    Integer setOnline(Users users);

//    查询房间的使用
    @Select("SELECT users.`tname`,users.`online` FROM users LEFT JOIN u_r  ON users.`uid`=u_r.`uid` WHERE rid=4 AND STATUS !=0  order by online")
    List<Users> selectOnlineStatus();
}
